//
//  CZDoujinCenterViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/18.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZDoujinCenterViewController.h"
#import "CZDoujinListViewController.h"
#import "CZNovelViewController.h"
#import "CZUploadPicViewController.h"
#import "CZUploadNovelViewController.h"

@interface CZDoujinCenterViewController ()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (nonatomic, copy) NSArray *btnArray;

@end

@implementation CZDoujinCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"同人分享";
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setTopBtns];
    [self setRightBarButton];
    CZDoujinListViewController *doujinListVC = [[CZDoujinListViewController alloc] init];
    [self addChildViewController:doujinListVC];
    self.contentView.height = Main_Screen_Height - 64 - 70;
    self.contentView.width = Main_Screen_Width;
    doujinListVC.type = @1;
    doujinListVC.view.frame = self.contentView.bounds;
    doujinListVC.doujinCollectionView.frame = self.contentView.bounds;
    doujinListVC.layoutView = self.contentView;

    [self.contentView addSubview:doujinListVC.view];
//    [doujinListVC postDoujinListDataWithType:@1];
    
    for (int i = 0; i < 3; i ++) {
        CZDoujinListViewController *doujinVC = [[CZDoujinListViewController alloc] init];
        [self addChildViewController:doujinVC];
        doujinVC.layoutView = self.contentView;
    }
    CZNovelViewController *novelVC = [[CZNovelViewController alloc] init];
    [self addChildViewController:novelVC];
    
    
    
//    _doujinListVC = doujinListVC;
    
    // Do any additional setup after loading the view from its nib.
}

- (void)setRightBarButton {
    
    UIButton *rightBarBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    rightBarBtn.frame = CGRectMake(0, 0, 54, 25);
    [rightBarBtn addTarget:self action:@selector(upload) forControlEvents:(UIControlEventTouchUpInside)];
    rightBarBtn.backgroundColor = mRGBColor(255, 174, 27);
    [rightBarBtn setTitle:@"上传" forState:(UIControlStateNormal)];
    rightBarBtn.titleLabel.textColor = [UIColor whiteColor];
    rightBarBtn.layer.cornerRadius = 2;
    rightBarBtn.clipsToBounds = YES;
//    rightBarBtn.tintColor = mRGBColor(255, 174, 27);
    rightBarBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarBtn];

}
- (void)setTopBtns {
    NSArray *titleArray = @[@"同人绘", @"cosplay", @"表情包", @"手办", @"故事"];
    NSArray *iconArray = @[@"icon_aniparo", @"icon_cosplay", @"icon_look", @"icon_garage", @"icon_novel"];
    NSArray *iconSelectArr = @[@"icon_aniparo_active", @"icon_cosplay_active", @"icon_look_active", @"icon_garage_icon", @"icon_novel_active"];
    NSMutableArray *tempArr = [[NSMutableArray alloc] init];
    UIView *backView = [[UIView alloc] init];
    [self.view addSubview:backView];
    backView.backgroundColor = mRGBColor(41, 101, 89);
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(64);
        make.height.equalTo(@70);
    }];
    for (int i = 0; i < 5; i++) {
        UIButton *button = [[UIButton alloc] init];
        [backView addSubview:button];
        [button setTitle:titleArray[i] forState:(UIControlStateNormal)];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [tempArr addObject:button];
        [button setTitleColor:mRGBColor(116, 155, 147) forState:(UIControlStateNormal)];
        
        [button addTarget:self action:@selector(selectTab:) forControlEvents:(UIControlEventTouchUpInside)];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
        
        [button setImage:[UIImage imageNamed:iconArray[i]] forState:(UIControlStateNormal)];
        [button setImage:[UIImage imageNamed:iconSelectArr[i]] forState:(UIControlStateSelected)];
        button.titleEdgeInsets = UIEdgeInsetsMake(50, -50, 15, 0);
        button.imageEdgeInsets = UIEdgeInsetsMake(10, 15, 35, 15);
        button.imageView.contentMode = UIViewContentModeScaleAspectFit;
        if (i == 0) {
            button.selected = YES;
        }
        if (i == 1) {
            button.titleEdgeInsets = UIEdgeInsetsMake(50, -55, 15, 0);
        }
        if (i == 2) {
            button.titleEdgeInsets = UIEdgeInsetsMake(50, -40, 15, 0);
        }
        if (i == 3) {
            button.titleEdgeInsets = UIEdgeInsetsMake(50, -45, 15, 0);
        }


    }
    
    self.btnArray = tempArr;
    [self.btnArray mas_distributeViewsAlongAxis:(MASAxisTypeHorizontal) withFixedItemLength:60 leadSpacing:0 tailSpacing:0];
    [self.btnArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView);
        make.height.equalTo(backView);
    }];
}
#pragma mark- actions
- (void)upload {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择上传类型" preferredStyle:(UIAlertControllerStyleAlert)];
    CZUploadPicViewController *uploadVC = [[CZUploadPicViewController alloc] init];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"上传同人绘" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        uploadVC.uploadTpye = @1;
        [self.navigationController pushViewController:uploadVC animated:YES];
        [alertVC dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"上传cosplay" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [alertVC dismissViewControllerAnimated:YES completion:nil];
        uploadVC.uploadTpye = @2;
        [self.navigationController pushViewController:uploadVC animated:YES];
    }];
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"上传表情包" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [alertVC dismissViewControllerAnimated:YES completion:nil];
        uploadVC.uploadTpye = @3;
        [self.navigationController pushViewController:uploadVC animated:YES];
    }];
    UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"上传手办" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [alertVC dismissViewControllerAnimated:YES completion:nil];
        uploadVC.uploadTpye = @4;
        [self.navigationController pushViewController:uploadVC animated:YES];
    }];
    UIAlertAction *action5 = [UIAlertAction actionWithTitle:@"上传故事" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        CZUploadNovelViewController *uploadVC = [[CZUploadNovelViewController alloc] init];
        [self.navigationController pushViewController:uploadVC animated:YES];
        [alertVC dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        [alertVC dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [alertVC addAction:action1];
    [alertVC addAction:action2];
    [alertVC addAction:action3];
    [alertVC addAction:action4];
    [alertVC addAction:action5];
    [alertVC addAction:cancelAction];

    [self presentViewController:alertVC animated:YES completion:nil];
}


- (void)selectTab:(UIButton *)button {
    if (button.selected) {
        return;
    }
    NSInteger index = [self.btnArray indexOfObject:button];
    for (UIButton *btn in self.btnArray) {
        if (btn != button) {
            btn.selected = NO;
        }else {
            btn.selected = YES;
        }
    }
    [self.contentView.subviews[0] removeFromSuperview];
    if (index < 4) {
        CZDoujinListViewController *doujinVC = self.childViewControllers[index];
        doujinVC.type = @(index + 1);
        doujinVC.view.frame = self.contentView.bounds;
        doujinVC.doujinCollectionView.frame = self.contentView.bounds;
        [self.contentView addSubview:doujinVC.view];
    } else {
        CZNovelViewController *novelVC = self.childViewControllers[index];
        novelVC.view.frame = self.contentView.bounds;
        novelVC.view.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:novelVC.view];
//        [novelVC postNovelData];
    }
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
